package com.yin.jia.liang.controller;

import com.github.pagehelper.PageInfo;
import com.yin.jia.liang.entity.Teach;
import com.yin.jia.liang.entity.User;
import com.yin.jia.liang.service.TeachPlService;
import com.yin.jia.liang.service.TeachService;
import com.yin.jia.liang.vo.TeachPlVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("teach")
public class TeachController {

    @Autowired
    TeachService teachService;
    @Autowired
    TeachPlService teachPlService;

    @RequestMapping(value = "{page}", method = RequestMethod.GET)
    public String list(@PathVariable Integer page, @ModelAttribute Teach teach, Model model) {
        PageInfo<Teach> pageInfo = teachService.pageFindByExample(page, teach);
        model.addAttribute("page", pageInfo);
        return "teach/list";
    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String list(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }
        List<Teach> teachs = teachService.findByUserId(user.getId());
        model.addAttribute("teachs", teachs);
        return "teach/own";
    }

    @RequestMapping(value = "show/{id}", method = RequestMethod.GET)
    public String show(@PathVariable Integer id, Model model) {
        Teach teach = teachService.findById(id);
        List<TeachPlVo> vo = teachPlService.findByTeachId(id);
        model.addAttribute("pl", vo);
        model.addAttribute("teach", teach);
        return "teach/show";
    }

    @RequestMapping(value = "add", method = RequestMethod.GET)
    public String index(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }
        return "teach/add";
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public String index(@ModelAttribute Teach teach, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }

        if (teach.getId() == null) {
            teach.setUserId(user.getId());
            teachService.save(teach);
        } else {
            teachService.update(teach);
        }
        if ("admin".equals(user.getUsername())) {
            return "redirect:/teach/1";
        }

        return "redirect:/teach/list";
    }

    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String update(HttpSession session, Model model, @PathVariable Integer id) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }
        Teach teach = teachService.findById(id);
        model.addAttribute("teach", teach);
        return "teach/add";
    }

    @RequestMapping(value = "del/{id}", method = RequestMethod.GET)
    public String del(@PathVariable Integer id, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }
        if ("admin".equals(user.getUsername())) {
            teachService.delete(id);
        } else {
            Teach t = teachService.findById(id);
            if (user.getId() == t.getUserId()) {
                teachService.delete(id);
            }
        }

        return "redirect:/teach/list";
    }
}
